package com.rmb.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.rmb.common.exception.ExceptionCast;
import com.rmb.common.model.response.CommonCode;
import com.rmb.dao.UserCompanyDao;
import com.rmb.mart.entity.userinfo.UserCompany;
import com.rmb.mart.utils.OssUtil;
import com.rmb.mart.utils.SendSms;
import com.rmb.request.UserCompanyRequest;
import com.rmb.service.UserCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (UserCompany)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 14:10:32
 */
@Service("userCompanyService")
public class UserCompanyServiceImpl implements UserCompanyService {
    @Resource
    private UserCompanyDao userCompanyDao;


    /**
     * 查询企业列表
     * 企业注册通过后进入企业总也列表里；
     * @return List<UserCompany>
     */
    @Override
    public List<UserCompany> findList() {
        return userCompanyDao.queryAll(new UserCompany());
    }


    /**
     * 添加企业并上传企业执照
     *
     * @param userCompany
     * @return
     */
    @Override
    public UserCompany addBusiness(UserCompany userCompany) {

        if (userCompany==null){
            ExceptionCast.cast(CommonCode.THIS_OBJECT_NULL);
        }
        UserCompany businessName = userCompanyDao.findByBusinessName(userCompany.getBusinessName());
        if (businessName!=null){
            //企业已经存在
            ExceptionCast.cast(CommonCode.BUSINESS_ALREADY_EXISTS);
        }

        if (userCompanyDao.insert(userCompany) == 0) {
            ExceptionCast.cast(CommonCode.FAIL);
        }
        return userCompany;
    }

    /**
     * 删除企业信息
     * @param id
     * @return
     */
    @Override
    public UserCompany deleteUserCompany(Long id) {
        UserCompany userCompany = userCompanyDao.queryById(id);
        if (userCompany == null ){
            ExceptionCast.cast(CommonCode.BUSINESS_NOT_EXISTS);
        }
        userCompany.setStatus(0);
        if (userCompanyDao.update(userCompany) == 0) {
            ExceptionCast.cast(CommonCode.FAIL);
        }
        return userCompany;
    }

    /**
     * 修改企业信息
     * @param userCompany
     * @return
     */
    @Override
    public UserCompany updateBusiness(UserCompany userCompany) {
        if (userCompany==null){
            ExceptionCast.cast(CommonCode.THIS_OBJECT_NULL);
        }
        if (userCompanyDao.update(userCompany) == 0) {
            ExceptionCast.cast(CommonCode.FAIL);
        }
        return userCompany;

    }

    /**
     * 上传图片
     * @param file
     * @return 图片路径
     */
    @Override
    public String uploadLicense(MultipartFile file) {
        return OssUtil.uploadFileInputSteam(file.getOriginalFilename(),file);
    }

    /**
     * 查询待审核的企业
     * @param status
     * @return
     */
    @Override
    public List<UserCompany> findByStatus(Integer status) {
        return userCompanyDao.findByStatus(status);
    }

    /**
     * 修改转态未审核未通过并发送消息提示用户
     *
     * @param userCompanyRequest
     * @return
     */
    @Override
    public int auditFailed(UserCompanyRequest userCompanyRequest) {
        if (userCompanyRequest==null){
            ExceptionCast.cast(CommonCode.THIS_OBJECT_NULL);
        }

        if (userCompanyRequest.messageIsNotNull()){
            SendSms.sendMag(String.valueOf(userCompanyRequest.getRegistrantPhone()),userCompanyRequest.getMessage());
        }
        return userCompanyDao.update(userCompanyRequest);
    }
}